package com.sgb.kjwl.view.ui.activity.self.pick;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityInvitationOfferBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.user.InvitationOfferEntity;
import com.sgb.kjwl.view.ui.adapter.self.pick.InvitationOfferAdapter;
import com.sgb.kjwl.viewmodel.self.pick.InvitationOfferModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

public class InvitationOfferActivity extends AppActivity {
    private ActivityInvitationOfferBinding binding;
    @Inject
    InvitationOfferModel viewModel;

    private InvitationOfferAdapter invitationOfferAdapter;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, InvitationOfferActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(InvitationOfferActivity.this, R.layout.activity_invitation_offer);
        binding.include.rightView.setVisibility(View.INVISIBLE);
        binding.setViewModel(viewModel);
    }

    @Override
    protected void init() {
        binding.include.ivBack.setOnClickListener(this);
        if (invitationOfferAdapter == null) {
            invitationOfferAdapter  = new InvitationOfferAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(invitationOfferAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getData("");
                binding.recyclerView.setLoadingMoreEnabled(true);
            }

            @Override
            public void onLoadMore() {
                if (invitationOfferAdapter.getDatas().size() < viewModel.total){
                    viewModel.getMoreData("");
                }else {
                    binding.recyclerView.loadMoreComplete();
                }
            }
        });

        viewModel.getData("");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }

    public void showData(List<InvitationOfferEntity> list){
        binding.recyclerView.refreshComplete();
        if (list.size() == 0){
            //binding.recyclerView.setVisibility(View.GONE);
            binding.emptyView.setVisibility(View.VISIBLE);
        }else {
            binding.recyclerView.setVisibility(View.VISIBLE);
            binding.emptyView.setVisibility(View.GONE);
        }
        invitationOfferAdapter.setDatas(list);
    }

    public void showMoreData(List<InvitationOfferEntity> list){
        binding.recyclerView.loadMoreComplete();
        binding.emptyView.setVisibility(View.GONE);
        invitationOfferAdapter.addDatas(list);
    }
}
